import os
import json
from datetime import datetime
from typing import List, Dict

# 使用内存存储日志而不是文件
logs_storage = []

def write_log(level: str, message: str):
    """写入日志到内存"""
    global logs_storage
    log_entry = {
        "timestamp": datetime.now().isoformat(),
        "level": level,
        "message": message
    }
    
    logs_storage.append(log_entry)
    
    # 只保留最近1000条日志
    if len(logs_storage) > 1000:
        logs_storage = logs_storage[-1000:]

def read_logs(limit: int = 50) -> List[Dict]:
    """从内存中读取日志"""
    global logs_storage
    # 返回最新的limit条日志
    return logs_storage[-limit:] if len(logs_storage) > limit else logs_storage